<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
	//登陆
	function login(){
		if(IS_POST){
            //验证码验证
    			$code = I('post.validateCode');
            	$v = new \Think\Verify();
            if(empty($code)){
            	$this->error('请填写验证码');
            }
            if(!$v->check($code)){
                $this->error('验证码错误');
            }
	            $name = I('post.username');
	            $pwd = I('post.password');
	            $user = M('user');
	            $data = $user->where('user_phon='.$name)->find();
            if(empty($data['user_phon'])){
            	$this->error('用户名或密码错误1');
            }else{
	            if(salt($pwd,$data['user_pwd_yan']) != $data['user_pwd']){
	            	$this->error('用户名或密码错误2');
	            }else{
	            	$_SESSION['name']  = $name;
	            	$this->success('登陆成功');
	            }
            }
        }else{
            $this->display();
        }
	}

	function zhuce(){
		if(IS_POST){
			$code = I('post.validateCode');
			$v = new \Think\Verify();
            if(empty($code)){
            	$this->error('请填写验证码');
            }
            if(!$v->check($code)){
                $this->error('验证码错误');
            }
            $phone = I('post.phone');
            $pwd = I('post.password');
            $p_code = I('post.phoneCode');
            if(session('code') != $p_code){
            	$this->error('手机验证码错误');
            }
            $phone = I('post.phone');
            $pwd = I('post.password');
            $salt = makeSalt();
            $user = M('user');
            $data['user_phon'] = $phone;
            $data['user_name'] = $phone;
            $data['user_pwd_yan'] = $salt;
            $data['user_pwd']  = salt($pwd,$data['user_pwd_yan']);
            $data['user_time']  = time();
            if($user->add($data)){
                $this->success('注册成功');
            }else{
                $this->error('注册失败');
            }
		}else{
			$this->display();
		}
		
	}

	//手机验证
	function phone($phone){
    	$code = rand(99999,11111);
    	$_SESSION['code'] =$code;
    	if(send_sms_code($phone,$code)){
    		echo json_encode(1);
    	}else{
    		echo json_decode(2);
    	}
	}

	function verify(){
        $config = array(
            'fontSize'  => '25',
            'useCurve'  => false,
            'useNoise'  => false,
            'length'    => '4',
            );   
        $ve = new \Think\Verify($config);
        $ve->entry();
    }
}
	